/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui;

import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JTextField;
import tal.drivechain.chain.AbortChainException;
import tal.drivechain.gui.GUIModule;
import tal.drivechain.matchers.VarMatcher;
import tal.drivechain.expression.dcExpression;

import tal.drivechain.plugin.Channel;
import tal.drivechain.variable.SetProperty;
import tal.drivechain.variable.SetProperty.Flag;
import tal.drivechain.variable.Variable;

/**
 *
 * @author Main
 */
public class Status extends GUIModule {
    private JTextField statusLabel;
    private VarMatcher trigger;
    private dcExpression expression;
    private dcExpression initStatus;
    private String currentValue;
    private boolean flashing;

    @Override
    public void setupProperties() throws IllegalArgumentException, AbortChainException {
        component = statusLabel = new JTextField();
        super.setupProperties();
        initStatus = (dcExpression)props.getProperty("initStatus");
        String initValue = (String)initStatus.evaluate(this);
        currentValue = initValue;
        statusLabel.setText(initValue);
        statusLabel.setHorizontalAlignment(JTextField.CENTER);
        statusLabel.setOpaque(true);
        statusLabel.setEditable(false);
        statusLabel.setBorder(BorderFactory.createEmptyBorder());
        if (props.hasProperty("columns")) {
            setColumns(props.getProperty("columns"), null);
        } else setColumns(initValue.length(), null);
    } 

    public void setTrigger(Object value, SetProperty.Flag flag) {
        trigger = (VarMatcher)value;
    } public Object getTrigger() { return trigger; }
    
    public void setExpression(Object value, SetProperty.Flag flag) {
        expression = (dcExpression)value;
    } public Object getExpression() { return expression; }

    public void setColumns(Object value, SetProperty.Flag flag) {
        if (statusLabel!=null) {
            statusLabel.setColumns((Integer)value);
            statusLabel.setText(makeDots(currentValue));
            relayout();
        }
    } public Object getColumns() { return statusLabel.getColumns(); }

    @Override
    public void setFont(Font value) {
        super.setFont(value);
        if (statusLabel!=null) {
            statusLabel.setFont(font);
            relayout();
        }
    }

    @Override
    public void setNegative(Boolean value) {
        super.setNegative(value);
        setColor(color);
    }

    @Override
    public void setColor(Color value) {
        super.setColor(value);
        if (statusLabel!=null) {
            if (flashing) {
                if (statusLabel.getForeground()==Color.BLACK)
                    statusLabel.setBackground(color);
                else if (statusLabel.getBackground()==Color.BLACK)
                    statusLabel.setForeground(color);
                else {
                    statusLabel.setBackground(color);
                    statusLabel.setForeground(Color.BLACK);
                }
            } else {
                if (negative) {
                    statusLabel.setBackground(Color.BLACK);
                    statusLabel.setForeground(color);
                } else {
                    statusLabel.setBackground(color);
                    statusLabel.setForeground(Color.BLACK);
                }
            }
            statusLabel.setSelectionColor(color.darker());
            statusLabel.repaint();
        }
    }

    public void setFlashing(Boolean property) {
        flashing = property;
        setColor(color);
    } public boolean getFlashing() { return flashing; }

    public void setInitStatus(dcExpression value) {
        initStatus = value;
    } public dcExpression getInitStatus() { return initStatus; }

    private void reverseLabel(JTextField label) {
        Color back = label.getForeground();
        Color fore = label.getBackground();
        label.setBackground(back);
        label.setForeground(fore);
        label.setSelectionColor(fore);
    }
    
    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        try {
            this.setInVars(in, timestamp, channel);
            if (trigger.matches(in, channel, this)) {
                currentValue = (String)expression.evaluate(this);
                statusLabel.setText(makeDots(currentValue));
                reverseLabel(statusLabel);
            }
         } catch (IllegalArgumentException ie) { log(ie.toString(), MSG.WARNING); }
        
    }

    private String makeDots(String s) {
        int size = statusLabel.getColumns() + 3;
        if (size<4) size = size * 2;
        if (s.length()>size)
            return s.substring(0, size) + "...";
        else return s;
    }
}
